// ContactPanel.cs
//
//  Copyright (C) 2008 Jacek Trubłajewicz
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.Drawing;
using Mono.Unix;
using NLog;

namespace wxGadu
{
	/// <summary>
	/// TextCtrl which allows only numeric values to be entered
	/// </summary>
	public class GGTextCtrl : wx.TextCtrl
	{
		#region data members
		
		#endregion
		
		public GGTextCtrl(wx.Window parent, int id):
			base(parent, id)
		{	
			EVT_KEY_DOWN(new wx.EventListener(OnKeyDown));
		}
		
		protected void OnKeyDown(object sender, wx.Event evt)
		{
			//TODO: need more work on this
			
			wx.KeyEvent kevt = (wx.KeyEvent) evt;
			if (kevt.KeyCode >= 48 && kevt.KeyCode <= 57)
				evt.Skip();
			if (kevt.KeyCode == (int) wx.KeyCode.WXK_DELETE || 
			    kevt.KeyCode == (int) wx.KeyCode.WXK_BACK ||
			    kevt.KeyCode == (int) wx.KeyCode.WXK_NEXT ||
			   // kevt.KeyCode ==  312||
			    kevt.KeyCode == (int) wx.KeyCode.WXK_END ||
			    kevt.KeyCode == (int) wx.KeyCode.WXK_HOME 
			    )
				evt.Skip();
			
			if ((kevt.KeyCode == 99 || kevt.KeyCode == 118) && kevt.ControlDown)
				evt.Skip();
				
		} 
	}
	
	
	public abstract class ContactDialog : wx.Dialog
	{
		#region Id
		protected enum Id
		{
			ggTC,
			nameTC,
			firstNameTC,
			lastNameTC,
			locationTC,
			phoneTC,
			photoButton		
		}
		#endregion

		#region sizers
		protected wx.BoxSizer topSizer;
		protected wx.BoxSizer buttonSizer;
		protected wx.BoxSizer photoSizer;
		protected wx.BoxSizer addInfoSizer;
		protected wx.BoxSizer mainInfoSizer;
		protected wx.BoxSizer leftAddSizer;
		protected wx.BoxSizer rightAddSizer;
		#endregion
		
		#region controls
		protected wx.StaticText ggST;
		protected wx.StaticText nameST;
		protected wx.StaticText firstNameST;
		protected wx.StaticText lastNameST;
		protected wx.StaticText locationST;
		protected wx.StaticText phoneST;
		protected GGTextCtrl ggTC;
		protected wx.TextCtrl nameTC;
		protected wx.TextCtrl firstNameTC;
		protected wx.TextCtrl lastNameTC;
		protected wx.TextCtrl locationTC;
		protected wx.TextCtrl phoneTC;
		protected wx.BitmapButton photoButton;
		protected wx.StaticBox addInfoBox;
		#endregion
		
		#region data members
		protected MainFrame mainFrame;
		#endregion
		
		public ContactDialog(wx.Window parent, int id, string title, Point pos, Size size, uint style):
			base(parent, id, title, pos, size, style)
		{
			mainFrame = (MainFrame) parent;
			
			DoControls();
			DoControlsProperties();
			DoLayout();
			
			EVT_BUTTON((int) Id.photoButton, new wx.EventListener(OnPhotoButtonClicked));
		}

		private void DoControls()
		{
			ggST =  new wx.StaticText(this, Catalog.GetString("GG:"));
			nameST = new wx.StaticText(this, Catalog.GetString("Name:"));
			firstNameST = new wx.StaticText(this, Catalog.GetString("Firstname:"));
			lastNameST = new wx.StaticText(this, Catalog.GetString("Lastname:"));
			locationST = new wx.StaticText(this, Catalog.GetString("Location:"));
			phoneST = new wx.StaticText(this, Catalog.GetString("Phone number:"));
			ggTC = new GGTextCtrl(this, (int) Id.ggTC);
			nameTC = new wx.TextCtrl(this, (int) Id.nameTC);
			firstNameTC = new wx.TextCtrl(this, (int) Id.firstNameTC);
			lastNameTC = new wx.TextCtrl(this, (int) Id.lastNameTC);
			locationTC = new wx.TextCtrl(this, (int) Id.locationTC);
			phoneTC = new wx.TextCtrl(this, (int) Id.phoneTC);
			photoButton = new wx.BitmapButton(this, (int) Id.photoButton, mainFrame.ImageMgr[ImageManager.NOBODY], wx.Window.wxDefaultPosition,
			                                  new Size(96, 96), wx.Window.wxNO_BORDER);
			addInfoBox = new wx.StaticBox(this, Catalog.GetString("Additional information"));
			
			
		}

		private void DoControlsProperties()
		{
			ggTC.ToolTip = Catalog.GetString("Gadu-Gadu number");
			nameTC.ToolTip = Catalog.GetString("Name of a contact");
			firstNameTC.ToolTip = Catalog.GetString("Firstname of a contact");
			lastNameTC.ToolTip = Catalog.GetString("Lastname of a contact");
			locationTC.ToolTip = Catalog.GetString("City/country of a contact");
			phoneTC.ToolTip = Catalog.GetString("Telephone number of a contact");
			photoButton.ToolTip = Catalog.GetString("Select avatar for a contact"); 
		}

		private void DoLayout()
		{
			topSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
			{
				photoSizer = new wx.BoxSizer(wx.Orientation.wxHORIZONTAL);
				{
					photoSizer.Add(photoButton, 0, wx.Direction.wxALL, 5);
					
					mainInfoSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
					{
						mainInfoSizer.Add(ggST, 0, 0);
						mainInfoSizer.Add(ggTC, 0, wx.Stretch.wxEXPAND);
						mainInfoSizer.Add(nameST, 0, 0);
						mainInfoSizer.Add(nameTC, 0, wx.Stretch.wxEXPAND);
					}
					photoSizer.Add(mainInfoSizer, 1, wx.Direction.wxALL|wx.Stretch.wxEXPAND, 5);
				}
				topSizer.Add(photoSizer, 0, wx.Stretch.wxEXPAND);

				addInfoSizer = new wx.StaticBoxSizer(addInfoBox, wx.Orientation.wxHORIZONTAL);
				{
					leftAddSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
					{
						leftAddSizer.Add(firstNameST, 0, 0);
						leftAddSizer.Add(firstNameTC, 0, wx.Stretch.wxEXPAND|wx.Direction.wxRIGHT, 5);
						leftAddSizer.Add(locationST, 0, 0);
						leftAddSizer.Add(locationTC, 0, wx.Stretch.wxEXPAND|wx.Direction.wxRIGHT, 5);
						leftAddSizer.Add(120, 40, 0, 0, 0);
					}
					addInfoSizer.Add(leftAddSizer, 1, wx.Stretch.wxEXPAND);

					rightAddSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
					{
						rightAddSizer.Add(lastNameST, 0, wx.Direction.wxLEFT, 5);
						rightAddSizer.Add(lastNameTC, 0, wx.Stretch.wxEXPAND|wx.Direction.wxLEFT, 5);
						rightAddSizer.Add(phoneST, 0, wx.Direction.wxLEFT, 5);
						rightAddSizer.Add(phoneTC, 0, wx.Stretch.wxEXPAND|wx.Direction.wxLEFT, 5);
						rightAddSizer.Add(120, 40, 0, 0, 0);
					}
					addInfoSizer.Add(rightAddSizer, 1, wx.Stretch.wxEXPAND);
				}
				topSizer.Add(addInfoSizer, 1, wx.Stretch.wxEXPAND|wx.Direction.wxALL, 5);

				buttonSizer = new wx.BoxSizer(wx.Orientation.wxHORIZONTAL);
				{
					buttonSizer.Add(20, 20, 1, 0, 0);
					//this buttons will be added in derived classes
				}
				topSizer.Add(buttonSizer, 0, wx.Stretch.wxEXPAND);
			}
			topSizer.Fit(this);
			topSizer.SetSizeHints(this);
			this.Sizer = topSizer;
		}
		
		protected virtual void OnPhotoButtonClicked(object sender, wx.Event evt)
		{
			string wildcard = String.Format(Catalog.GetString("JPG files ({0})|{0}|PNG files ({1})|{1}|GIF files ({2})|{2}|BMP files ({3})|{3}"),
			                                "*.jpg", "*.png", "*.gif", "*.bmp");
			
			wx.FileDialog photoDlg = new wx.FileDialog(this, 
			                                           Catalog.GetString("Choose a file"), 
			                                           Environment.GetFolderPath(Environment.SpecialFolder.Personal), 
			                                           string.Empty, 
			                                           wildcard,
			                                           wx.FileDialogStyle.wxOPEN
			                                           );
			
			if (photoDlg.ShowModal() == wx.Window.wxID_OK)
			{
				try
				{
					photoButton.BitmapLabel = new wx.Bitmap(Helper.LoadAndScale(photoDlg.Path, 96));
				}
				catch (ArgumentException e)
				{
					wx.MessageDialog md = new wx.MessageDialog(this, e.Message, Catalog.GetString("File not loaded"),
			                                           wx.MessageDialog.wxOK|wx.MessageDialog.wxICON_ERROR);
					md.ShowModal(); 
				}
			}
		}
	}
}
